package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IAgencySalaryHistoryDao;
import com.boss.salary.vo.SalaryColumnWithStateVO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Repository
public class AgencySalaryHistoryDaoImpl extends BaseDaoImpl implements IAgencySalaryHistoryDao {

    @Override
    public List<Map<String, Object>> selectSalaryColumnAndName(String province,String agencyId, String month) {
        if (StringUtils.isEmpty(agencyId) || StringUtils.isEmpty(month)) {
            return Collections.emptyList();
        }
        StringBuilder sb = new StringBuilder();
        List<Object> conditions = new ArrayList<>();
        sb.append(" select ")
                .append(" sba.id salaryId, sba.salary_name salaryName ")
                .append(" ,sba.salary_code columnName,sba.salary_code currentColumn")
                //.append("ashy.column_name columnName, asy.column_name currentColumn ")
                .append(" from ")
//                .append(TableName.AGENCY_SALARY_HISTORY).append(" ashy ")
//                .append(" join ")
//                .append(TableName.AGENCY_SALARY).append(" asy ")
//                .append(" on ").append(" asy.salary_id = ashy.salary_id and asy.is_deleted = 0 AND asy.agency_id = ? ");
//        conditions.add(agencyId);
//        sb.append(" join ")
                .append(TableName.SALARY_BASEDATA).append(" sba ")
               // .append(" on ").append(" ashy.salary_id = sba.id and sba.is_deleted = 0 ")
                .append(" where ")
                .append(" sba.is_deleted = 0 ")
                .append(" and sba.mof_div_code=? ");
            conditions.add(province);
                //.append("and ashy.agency_id = ? and ashy.salary_month = ? ");
//        conditions.add(agencyId);
//        conditions.add(month);
        sb.append(" order by sba.show_order ");
        return super.getjdbcTemplate().queryForList(sb.toString(),conditions.toArray());
    }

    @Override
    public List<SalaryColumnWithStateVO> selectSalaryColumnWithStateVO(String agencyId, String salaryMonth, List<String> salaryItem,String mofDivCode) {
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" select ")
                .append(" sba.is_accsal_pay, sba.is_accsal_withhold, sba.salary_code column_name ")
                .append(" from ")
//                .append(TableName.AGENCY_SALARY_HISTORY).append(" ashy ")
//                .append(" join ")
                .append(TableName.SALARY_BASEDATA).append(" sba ")
//                .append(" on ").append(" ashy.salary_id = sba.id and sba.base_state = '1' and sba.is_deleted = 0 ")
                .append(" where ")
                .append(" sba.is_deleted=0");
//                .append(" ashy.is_deleted = 0 and ashy.salary_month = ? and ashy.agency_id = ? ");
//        params.add(salaryMonth);
//        params.add(agencyId);
        if(!StringUtils.isEmpty(mofDivCode)){
            sb.append(" and sba.mof_div_code=?");
            params.add(mofDivCode);
        }
        sb.append(" and sba.salary_code in (");
        for(String column : salaryItem){
            if(salaryItem.indexOf(column) != salaryItem.size() - 1){
                sb.append(" ?, ");
            }else{
                sb.append(" ?) ");
            }
            params.add(column);
        }
        return super.getjdbcTemplate().query(sb.toString(), params.toArray(),
                new BeanPropertyRowMapper<>(SalaryColumnWithStateVO.class));
    }
}
